<?php
// 检查菜单结构字段
$host = '127.0.0.1';
$port = 3306;
$database = 'qiandao';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 检查菜单结构字段 ===\n\n";
    
    // 检查表结构
    echo "1. 检查system_menu表结构:\n";
    $stmt = $pdo->query("SHOW COLUMNS FROM system_menu");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $hasSpt = false;
    $hasSpc = false;
    $hasSpp = false;
    $hasSpl = false;
    
    foreach ($columns as $col) {
        if ($col['Field'] == 'spt') $hasSpt = true;
        if ($col['Field'] == 'spc') $hasSpc = true;
        if ($col['Field'] == 'spp') $hasSpp = true;
        if ($col['Field'] == 'spl') $hasSpl = true;
    }
    
    echo "  spt字段: " . ($hasSpt ? '存在' : '不存在') . "\n";
    echo "  spc字段: " . ($hasSpc ? '存在' : '不存在') . "\n";
    echo "  spp字段: " . ($hasSpp ? '存在' : '不存在') . "\n";
    echo "  spl字段: " . ($hasSpl ? '存在' : '不存在') . "\n";
    
    // 查询打卡积分菜单的详细信息
    echo "\n2. 打卡积分菜单详细信息:\n";
    $stmt = $pdo->query("SELECT * FROM system_menu WHERE title = '打卡积分'");
    $menu = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($menu) {
        echo "  ID: {$menu['id']}\n";
        echo "  PID: {$menu['pid']}\n";
        echo "  Title: {$menu['title']}\n";
        echo "  Status: {$menu['status']}\n";
        echo "  Sort: {$menu['sort']}\n";
        echo "  URL: {$menu['url']}\n";
        echo "  Node: {$menu['node']}\n";
        
        if ($hasSpt) echo "  SPT: " . ($menu['spt'] ?? 'NULL') . "\n";
        if ($hasSpc) echo "  SPC: " . ($menu['spc'] ?? 'NULL') . "\n";
        if ($hasSpp) echo "  SPP: " . ($menu['spp'] ?? 'NULL') . "\n";
        if ($hasSpl) echo "  SPL: " . ($menu['spl'] ?? 'NULL') . "\n";
    } else {
        echo "  ✗ 未找到打卡积分菜单！\n";
    }
    
    // 对比其他一级菜单
    echo "\n3. 所有一级菜单对比:\n";
    $stmt = $pdo->query("SELECT id, title, status, sort, url, node FROM system_menu WHERE pid = 0 ORDER BY sort DESC, id ASC");
    $topMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($topMenus as $m) {
        $statusText = $m['status'] == 1 ? '启用' : '禁用';
        echo "  ID:{$m['id']} {$m['title']} (状态:{$statusText}, 排序:{$m['sort']}, URL:{$m['url']}, Node:{$m['node']})\n";
    }
    
    // 检查是否需要更新spt/spc等字段
    if (!$hasSpt || !$hasSpc) {
        echo "\n⚠️ 警告: system_menu表缺少spt/spc等字段！\n";
        echo "这些字段是菜单树结构必需的字段。\n";
    }
    
} catch (PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

